<template>
  <div class="change-password-container">
    <el-card class="change-password-card">
      <h2>修改密码</h2>
      <el-form :model="form" ref="formRef" label-width="100px">
        <el-form-item label="原密码">
          <el-input v-model="form.password" placeholder="请输入原密码" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="form.newpassword" placeholder="请输入新密码" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认新密码">
          <el-input v-model="form.confirmPassword" placeholder="请再次输入新密码" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">提交</el-button>
        </el-form-item>
      </el-form>
      <el-alert v-if="msg" :title="msg" type="info" show-icon style="margin-top: 10px;" />
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const form = ref({
  password: '',
  newpassword: '',
  confirmPassword: ''
})
const msg = ref('')
const formRef = ref(null)
const orgId = localStorage.getItem('loginId')
const handleSubmit = async () => {
  if (form.value.newpassword !== form.value.confirmPassword) {
    msg.value = '两次输入的新密码不一致'
    ElMessage.error(msg.value)
    return
  }
  try {
    const response = await axios.put(
      `http://localhost:8080/orgs/${form.value.password}/${form.value.newpassword}/editPassword`,
      {
        "id": Number(orgId)
      }
    );
    if (response.data && response.data.code === 200) {
      msg.value = '密码修改成功！';
      ElMessage.success('密码修改成功');
    } else {
      msg.value = response.data.msg || '密码修改失败';
      ElMessage.error(msg.value);
    }
  } catch (error) {
    msg.value = '网络错误，修改失败';
    ElMessage.error(msg.value);
  }
}
</script>

<style scoped>
.change-password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 60vh;
}
.change-password-card {
  width: 400px;
  padding: 30px 20px;
}
</style>